package com.maxd.adapter;

import com.maxd.model.CommonInfo;
import com.maxd.respository.common.CommonRepository;
import com.maxd.utils.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import weixin.popular.support.TokenManager;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

@Component
public class TokenManagerListener implements ServletContextListener {
    @Autowired
    private CommonRepository commonRepository;
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        //WEB容器 初始化时调用
         //微信appid
        CommonInfo one = commonRepository.findOne(5);
        //微信秘钥
        CommonInfo two = commonRepository.findOne(21);


        //微信小程序appid
//        CommonInfo three = commonRepository.findOne(45);
//        //微信小程序秘钥
//        CommonInfo four = commonRepository.findOne(46);
        TokenManager.setDaemon(false);
        TokenManager.init(one != null ? one.getValue() : Config.appId, two != null ? two.getValue() : Config.appSecret);
        //获取微信小程序的appid
//        TokenManager.init(three != null ? three.getValue() : onfig.mpAppId, four != null ? four.getValue() : Config.mpAppSecret);

//        TokenManager.init("wx4689cd2a5297713d","2bbef3d7dac9b9f0ad4146ac442c4fdd");//go 灵系生活
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        //WEB容器  关闭时调用
        TokenManager.destroyed();
    }
}
